package edu.must.uh.servlet;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.alibaba.fastjson.JSON;
import edu.must.uh.Filter.User;
import edu.must.uh.Generator.JavaBeanFileUtils;
import edu.must.uh.Generator.JavaImplFileUtils;
import edu.must.uh.Generator.JavaServletFileUtils;
import edu.must.uh.impl.BaseUserExImpl;
import edu.must.uh.util.CookieUtil;
import edu.must.uh.util.EncryptUtil;
import edu.must.uh.util.JedisUtil;
import edu.must.uh.util.LogHelper;
import edu.must.uh.util.R;
import edu.must.uh.util.Tools;
import edu.must.uh.util.ToolsOfDate;
import edu.must.uh.util.ToolsOfString;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/LoginServlet")
public class LoginServlet extends  BaseServlet {
	private static final long serialVersionUID = 1L;
    BaseUserExImpl BaseUserimplEx = new BaseUserExImpl();
	
    
    public Object dologinOut(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String username=ToolsOfString.conveString(request.getParameter("username")).toUpperCase() ;
        JedisUtil.del("LoginUser",username);   
        return R.succ();        
    }	

    public Object dologin(HttpServletRequest request, HttpServletResponse response) throws Exception {
        
        //   JavaBeanFileUtils.updateJavaPOFile();
        //   JavaImplFileUtils.updateJavaPOFile();
       //    JavaServletFileUtils.updateJavaPOFile();           

           
	        String username=ToolsOfString.conveString(request.getParameter("username")).toUpperCase() ;
	        String password=ToolsOfString.conveString(request.getParameter("password"));	 
	        User user = new User();
	        user.setUsername(username);
	        user.setPassword(password);		        
	        user.setLoginTime(ToolsOfDate.getCurrDateTime());
	        user.setLocalIp(Tools.getLocalIp());
	        Map<String, Object> resMap = BaseUserimplEx.getUserByUserName(username);
	        if(resMap.isEmpty()) {
	            
	          //  LogHelper.saveLonginInfoLog(request,username, "無此用戶");
	            return  R.fail("無此用戶");
            }	
	       	else 
	        {	       	   
	            String mPassword =ToolsOfString.NulltoEmpty(resMap.get("Password"));
	            String Secretkey =ToolsOfString.NulltoEmpty(resMap.get("Secretkey"));
	            
	            String userId =ToolsOfString.NulltoEmpty(resMap.get("UserId"));
	            boolean isSystem =username.toUpperCase().equals("SYSTEM");
	            String tPassword=EncryptUtil.getInstance().MD5(password, Secretkey);
	            if(mPassword.equals(tPassword)) {  
	                String keyString = UUID.randomUUID().toString();
                    user.setEncryptkey(keyString);  
                    user.setUserId(userId);
                    user.setIsSystem(isSystem);
                  //  JedisUtil.set("LoginUser",username, JSON.toJSONString(user)); 
                    
                  //  Map<String,String> loginMap= new HashMap<String, String>();
                  //  loginMap.put("LoginUser", username);
                   // loginMap.put("cookieskey", keyString);
                    
                  //  CookieUtil.addCookie(response, "LoginUser", username, 0);
                  //  CookieUtil.addCookie(response, "cookieskey", keyString, 0);
                    
                   // LogHelper.saveLonginInfoLog(request,username, "登錄成功");
                    
                    
                    return R.succ("登錄成功");
	            }else {
	           //    LogHelper.saveLonginInfoLog(request,username, "密碼錯誤");
	               return R.fail("密碼錯誤");
	            }	            
	        }
	    }

}
